package com.hd.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class CommonUtil {
	public static Double parseDouble(String s, double defaultValue) {
		try {
			return Double.parseDouble(s);
		} catch (Exception e) {
			return defaultValue;
		}
	}

	public static Long parseLong(String s, long defaultValue) {
		try {
			return Long.parseLong(s);
		} catch (Exception e) {
			return defaultValue;
		}
	}
	public static Double parseDouble(String s) {
		return parseDouble(s,0);
	}
	public static Long parseLong(String s) {
		return parseLong(s,0);
	}
	public static String join(Iterator<? extends Object> it,char sep){
		if(it == null || !it.hasNext())
			return null;
		StringBuffer sb = new StringBuffer();
		while(it.hasNext()){
			Object o = it.next();
			sb.append(o.toString()).append(sep);
		}
		String s = sb.substring(0,sb.length()-1);
		return s;
	}
	public static void main(String[] args) {
		List<String> arrs = new ArrayList<String>();
		arrs.add("12");
		arrs.add("22");
		arrs.add("32");
		System.out.println(CommonUtil.join(arrs.iterator(),','));
	}
	public static Date parseDate(String format,String date){
		try {
			return new SimpleDateFormat(format).parse(date);
		} catch (Exception e) {
			return null;
		}
	}
	public static String formatDate(String format,Date date){
		return new SimpleDateFormat(format).format(date); 
	}
}

